/*
 * Jenny Ruppen 09-730-391
 * Sebastien Goldberg 08-914-855
 * Henry Raymond 08-928-061
 * Stefan Mussato 09-752-270
 */
 
package ch.uzh.ifi.sopra.gruppe9.game;

import java.rmi.RemoteException;
import java.util.ArrayList;


/**
 * Client-side stub of IPlayer class. It catches all the exceptions thrown 
 * by the RMI methods.
 */
public class PlayerStub {
	
	
	/**
	 * Actual RMI object
	 */
	private IRmiPlayer player;
	
	
	/**
	 * Constructor: sets actual RMI object
	 */
	public PlayerStub(IRmiPlayer player) {
		this.player = player;
	}
	

	/**
	 * Setter for player name
	 */
	public void setName(String name) {
		try {
			player.setName(name);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
	

	/**
	 * Getter for player name
	 */
	public String getName() {
		String name = "";
		try {
			name = player.getName();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return name;
	}
	

	/**
	 * Setter for player color
	 */
	public void setColor(int color) {
		try {
			player.setColor(color);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * Getter for player color
	 */
	public int getColor() {
		int color = 0;
		try {
			color = player.getColor();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return color;
	}
	
	
	/**
	 * Get player's prestige point score
	 */
	public int getPoints() {
		int points = 0;
		try {
			points = player.getPoints();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return points;
	}

	
	/**
	 * Sets if player is ready to start a new game
	 */
	public void setReady(boolean ready) {
		try {
			player.setReady(ready);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * Returns true if player ready to start a new game 
	 */
	public boolean isReady() {
		boolean ready = false;
		try {
			ready = player.isReady();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return ready;	
	}
	
	
	/**
	 * Returns how much money the player has.
	 */
	public int getMoneyBalance() {
		int money = 0;
		try {
			money = player.getMoneyBalance();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return money;
	}
	
	
	/**
	 * Get list of buying agent cards on the player's hand.
	 */
	public ArrayList<BuyingAgent> getBuyingAgents() {
		ArrayList<BuyingAgent> buyingAgents = new ArrayList<BuyingAgent>();
		try {
			for(int id : player.getBuyingAgentIDs()) {
				buyingAgents.add(new BuyingAgent(id));
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return buyingAgents;
	}
	
	
	/**
	 * Returns a list of all tower elements in the inventory of this player.
	 */
	public ArrayList<TowerElement> getInventory() {
		ArrayList<TowerElement> inventory = new ArrayList<TowerElement>();
		try {
			for(int id : player.getInventoryIDs()) {
				inventory.add(new TowerElement(id));
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return inventory;
	}
	
	
	/**
	 * Returns a list of all towers of this player.
	 */
	public ArrayList<Tower> getTowers() {
		ArrayList<Tower> towers = new ArrayList<Tower>();
		String instructions = "";
		try {
			instructions = player.getTowerInstructions();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		String[] towerInstructions = instructions.split(";");
		for(String instruction : towerInstructions) {
			if(!instruction.equals("")) {
				Tower newTower = new Tower();
				newTower.assemble(instruction);
				towers.add(newTower);
			}
		}
		return towers;
	}
	
}
